package com.lanou3g.dllo.baidumusic.mine.myfavorite;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.database.MyFavorite;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;

import java.util.ArrayList;

/**
 * Created by dllo on 16/7/6.
 */
public class MyFavoriteAdapter extends BaseAdapter{

    private ArrayList<MyFavorite> datas;
    private Context context;
    private MyRvOnClickListener myRvOnClickListener;

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    public MyFavoriteAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(ArrayList<MyFavorite> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size() > 0 && datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.size() > 0 && datas != null ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recentlyplay,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.recentlyPlaySongsName.setText(datas.get(position).getTitle());
        holder.recentlyPlaySinger.setText(datas.get(position).getAuther());
        holder.recentlyPlayMoreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myRvOnClickListener.onClick(position);
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView recentlyPlaySongsName,recentlyPlaySinger;
        ImageView recentlyPlayMoreBtn;

        public ViewHolder(View view) {

            recentlyPlaySongsName = (TextView) view.findViewById(R.id.recentlyplay_songsname);
            recentlyPlaySinger = (TextView) view.findViewById(R.id.recentlyplay_singer);
            recentlyPlayMoreBtn = (ImageView) view.findViewById(R.id.recentlyplay_more_btn);
        }
    }
}
